<template>
  <div class="login-container">
    <div class="left-box">
      <!-- 标题区域 -->
      <div class="title">
        <img src="@/assets/login.png" alt="" />
        <span class="left_title">黑马面面</span>
        <span class="line"></span>
        <span class="right_title">用户登陆</span>
      </div>
      <!-- 表单区域 -->
      <el-form ref="form" :model="form" :rules="rules" class="login">
        <!-- 手机号输入框 -->
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            v-model="form.phone"
          >
          </el-input>
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
            show-password
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="form.password"
          >
          </el-input>
        </el-form-item>
        <!-- 验证码输入框 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <el-input
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                v-model="form.code"
              >
              </el-input>
            </el-col>
            <el-col :span="7">
              <img
                :src="loginUrl"
                class="login-code"
                @click="changeLoginCode"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 单选框文字部分 -->
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked">
            <el-link :underline="false" type="info">我已阅读并同意</el-link>
            <el-link type="primary">用户协议</el-link>
            <el-link :underline="false" type="info">和</el-link>
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <!-- 登陆注册按钮 -->
        <el-form-item>
          <el-button type="primary" id="my-btn" @click="submitForm('form')"
            >登陆</el-button
          >
          <el-button type="primary" id="my-btn_" @click="showRegister"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/login_banner_ele.png" alt="" />
    <!-- 注册表单区域 -->
    <register ref="register"></register>
  </div>
</template>

<script>
// 导入注册组件
import register from './register.vue'
// 引入登录接口
import { login } from '@/api/login.js'
// 导入token工具函数
import { setToken } from '../../utils/token.js'
export default {
  components: {
    register
  },
  data () {
    return {
      // 登录验证码图片地址
      loginUrl: process.env.VUE_APP_BaseURL + '/captcha?type=login',
      form: {
        phone: 18833333333,
        password: 123456,
        code: '',
        checked: []
      },
      // 配置校验规则
      rules: {
        phone: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式有误',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 12,
            message: '密码长度为 6 到 12',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: 'blur'
          },
          {
            min: 4,
            max: 4,
            message: '验证码长度为4',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            // 必须有内容,而不是布尔值
            required: true,
            message: '必须同意用户协议',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    // 获取登录验证码
    changeLoginCode () {
      this.loginUrl =
        process.env.VUE_APP_BaseURL + '/captcha?type=login&t=' + Date.now()
    },
    // 显示注册框
    showRegister () {
      this.$refs.register.dialogFormVisible = true
    },
    // 提交登录信息
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // this.$message.success('登录成功')
          // 获取表单登录信息
          const { code, password, phone } = this.form
          // console.log(code, password, phone)
          // 根据数据调用登录接口
          login({ code, password, phone })
            .then(res => {
              this.$message.success('登录成功')
              // // 设置token
              setToken(res.data.data.token)
              // // 跳转页面
              this.$router.push('/index')
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          this.$message.error('登录失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100vh;
  background: linear-gradient(#01c6fa 28%, #1493fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left-box {
    width: 478px;
    height: 550px;
    opacity: 1;
    background: #f5f5f5;
    box-sizing: border-box;
    padding-top: 44px;
    padding-left: 42px;
    padding-right: 42px;
    .title {
      display: flex;
      align-items: center;
      padding-left: 48px;
      margin-bottom: 30px;
      img {
        margin-right: 12px;
        margin-top: 3px;
      }
      .left_title {
        font-size: 24px;
      }
      .line {
        height: 30px;
        background-color: #dedede;
        width: 1px;
        margin-right: 12px;
        margin-left: 12px;
      }
      .right_title {
        font-size: 22px;
      }
    }
    .login {
      .el-button {
        width: 100%;
      }
      #my-btn_ {
        margin-left: 0;
      }
      #my-btn {
        margin-bottom: 20px;
      }
    }
    .login-code {
      display: block;
      width: 100%;
      height: 40px;
    }
  }
}
</style>
